package com.eastfair.projectcore.controller;

import com.eastfair.model.TenantRuleVO;
import com.eastfair.projectcore.dto.*;
import com.eastfair.projectcore.entity.TenantRule;
import com.eastfair.projectcore.service.TenantRuleService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;


/**
 * <p>
 * 前端控制器
 * 租户规则表（合并数据用）
 * </p>
 *
 * @author ligang
 * @date 2022-07-12
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/tenantRule")
@Api(value = "TenantRule", tags = "租户规则表（合并数据用）")
public class TenantRuleController extends SuperController<TenantRuleService, Long, TenantRule, TenantRulePageQuery, TenantRuleSaveDTO, TenantRuleUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<TenantRule> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<TenantRule> tenantRuleList = list.stream().map((map) -> {
            TenantRule tenantRule = TenantRule.builder().build();
            //TODO 请在这里完成转换
            return tenantRule;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(tenantRuleList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<TenantRulePageQuery> params) {
        TenantRulePageQuery pageQuery = params.getModel();
    }


    /**
     * 执行子系统共享表
     *
     * @param tenantRuleDTO 项目实体
     * @return
     */
    @ApiOperation(value = "执行子系统共享表", notes = "执行子系统共享表")
    @PostMapping("/executeTenantTable")
    public R executeTenantTable(@RequestBody TenantRuleDTO tenantRuleDTO) {
        return baseService.executeTenantTable(tenantRuleDTO.getSubSystemIds(),tenantRuleDTO.getTableName());
    }

    /**
     * 查询全量数据
     */
    @ApiOperation(value = "查询全量数据", notes = "查询全量数据")
    @GetMapping("/queryTenantTable")
    public R<List<TenantRuleVO>> queryTenantTable() {
        return baseService.queryTenantTable();
    }

}
